﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Kojarskiy.Models;
using SiteMap = Kojarskiy.Models.SiteMap;

namespace Kojarskiy.Core
{
    public static class SiteMapHelper
    {

        public static string GetAbsoluteUrl(string url)
        {
            return "http://" + HttpContext.Current.Request.Url.Authority + "/" + url;
        }

        public static List<SiteMap> GetSiteMenu()
        {
            var context = new KojarskiyDbEntities();
            var list = context.SiteMap.Where(x => x.ParentId < 2 && x.Show == true).OrderBy(x => x.Order).ToList();
            return list;
        }

        public static List<SiteMap> GetNodes()
        {
            var context = new KojarskiyDbEntities();
            return context.SiteMap.Where(x => x.ParentId != null).ToList();
        }

        public static SiteMap GetSiteMapNodeNameById(int siteMapId)
        {
            var context = new KojarskiyDbEntities();
            return context.SiteMap.FirstOrDefault(x => x.SiteMapId == siteMapId);
        }

        public static SiteMap GetRootNode()
        {
            var context = new KojarskiyDbEntities();
            return context.SiteMap.FirstOrDefault(x => x.ParentId == null);
        }

        public static SiteMap GetSiteMapNodeByUrl(string url)
        {
            try
            {
                var context = new KojarskiyDbEntities();
                var parts = url.TrimStart('/').TrimEnd('/').Split('?');
                var query = parts[0];
                var node = context.SiteMap.Where(x => x.Path == query).FirstOrDefault();
                if (node == null) node = GetRootNode();
                return node;
            }
            catch (Exception ex)
            {
                KojarskiyApplication.Log(KojarskiyApplication.ErrorType.Fatal, ex.Message, ex.StackTrace);
                return GetRootNode();
            }
        }
        public static List<SiteMap> GetSiteTree(string part)
        {
            var context = new KojarskiyDbEntities();
            var list = context.SiteMap.Where(x => x.ParentId == null && x.Culture == Localization.Current).OrderBy(x => x.Order).ToList();
            return list;
        }
        public static List<SiteMap> GetChildNodes(SiteMap node)
        {
            var context = new KojarskiyDbEntities();
            var list = context.SiteMap.Where(x => x.ParentId == node.SiteMapId).OrderBy(x => x.Order).ToList();
            return list;
        }
        public static string GetNodePathString(int? nodeId)
        {
            var context = new KojarskiyDbEntities();
            var nodePaths = new List<string>();
            while (nodeId != null)
            {
                var item = context.SiteMap.Where(x => x.SiteMapId == nodeId).First();
                string link = "<a href=\"" + GetAbsoluteUrl(item.Path) +"\">"+ item.PageName +"</a>";
                nodePaths.Add(link);
                nodeId = item.ParentId;
            }
            nodePaths.Reverse();
            string result = "";
            foreach (var nodePath in nodePaths)
            {
                result += nodePath + " | ";
            }
            return result.TrimEnd(new []{'"',' ','|','"'});
        }
    }
}